/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qp.smsmarketing.view.mbean.abstractmbean;

import com.qp.smsmarketing.dao.UsuarioDAO;
import com.qp.smsmarketing.entity.OperacaoUsuario;
import com.qp.smsmarketing.entity.Usuario;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JTable;

/**
 *
 * @author waldenilson
 */
public abstract class AbstractMBean implements IMBean {

    protected String jbuttonGroupSelected(ButtonGroup bgGroup) {
        String res = "";
        //Pega todos os RadioButtons do GroupButton
        Enumeration<AbstractButton> elements = bgGroup.getElements();
        //Coloca todos os elementos em uma lista
        List<AbstractButton> listaButtons = Collections.list(elements);
        //LaÃ§o para percorrer todos os JRadioButtons e verificar qual estÃ¡ selecionado
        for (int i = 0; i < listaButtons.size(); i++) {
            if (bgGroup.isSelected(listaButtons.get(i).getModel())) {
                res = listaButtons.get(i).getActionCommand();
            }
        }
        return res;
    }

    protected boolean verificarPermissaoOperacao(int idUsuario, String operacao) {
        if (idUsuario != 0) {
            UsuarioDAO udao = new UsuarioDAO();
            Usuario u = (Usuario) udao.buscarRegistro(idUsuario);
            boolean ok = false;
            for (OperacaoUsuario ou : u.getOperacaoUsuarioList()) {
                if (ou.getOperacaoOperacaoUsuario().getNomeOperacao().
                        equals(operacao)) {
                    ok = true;
                }
            }
            return ok;
        } else {
            return true;
        }
    }

    protected void carregarView(JDialog jf, JDialog jf2) {
        if (jf == null) {
            jf = jf2;
        }
        jf.setVisible(true);

    }

    protected JTable ocultarColuna(JTable jt, int coluna) {
        jt.getColumnModel().getColumn(coluna).setMaxWidth(0);
        jt.getColumnModel().getColumn(coluna).setMinWidth(0);
        jt.getTableHeader().getColumnModel().getColumn(coluna).setMaxWidth(0);
        jt.getTableHeader().getColumnModel().getColumn(coluna).setMinWidth(0);

        return jt;
    }

    protected void internacionalizar(String lang, String country) {
        Locale.setDefault(new Locale(lang, country));
        ResourceBundle.getBundle("com.qp.smsmarketing.internationalization.Bundle", Locale.getDefault());
    }

    public String getStringBundle(String key) {
        return ResourceBundle.getBundle("com.qp.smsmarketing.internationalization.Bundle").getString(key);
    }

    public String getStringConfigModem(String key) {
        Properties properties = new Properties();
        String res = "";
        try {
            File f = new File("ConfigModem.properties");
            if (!f.exists()) {
                criarConfigModem();
            }
            //Setamos o arquivo que será lido
            FileInputStream fis = new FileInputStream(
                    "ConfigModem.properties");
            //método load faz a leitura através do objeto fis
            properties.load(fis);
            res = properties.getProperty(key);
        } catch (IOException e) {
            criarConfigModem();
        }

        return res;
    }

    public void setStringConfigModem(String key, String value) {
        Properties properties = new Properties();
        properties.setProperty(key, value);

        try {
            //Criamos um objeto FileOutputStream             
            FileOutputStream fos = new FileOutputStream(
                    "ConfigModem.properties");
            //grava os dados no arquivo
            properties.store(fos, "FILE MODEM PROPERTIES:");
            //fecha o arquivo
            fos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void criarConfigModem() {
        Properties properties = new Properties();
        properties.setProperty("SimPin", "0000");

        try {
            //Criamos um objeto FileOutputStream             
            FileOutputStream fos = new FileOutputStream(
                    "ConfigModem.properties");
            //grava os dados no arquivo
            properties.store(fos, "FILE MODEM PROPERTIES:");
            //fecha o arquivo
            fos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
    
    
    public String getStringConfigApp(String key) {
        Properties properties = new Properties();
        String res = "";
        try {
            File f = new File(getClass().getResource("/com/qp/smsmarketing/utils/ConfigApp.properties").toURI());
            if (!f.exists()) {
               // criarConfigModem();
            }
            //Setamos o arquivo que será lido
            FileInputStream fis = new FileInputStream(
                    f);
            //método load faz a leitura através do objeto fis
            properties.load(fis);
            res = properties.getProperty(key);
        } catch (Exception e) {
            criarConfigModem();
        }

        return res;
    }
    
    public void setStringConfigApp(String key, String value) {
        Properties properties = new Properties();
        properties.setProperty(key, value);

        try {
            //Criamos um objeto FileOutputStream             
            File f = new File(getClass().getResource("/com/qp/smsmarketing/utils/ConfigApp.properties").toURI());
            FileOutputStream fos = new FileOutputStream(
                    f);
            //grava os dados no arquivo
            properties.store(fos, "FILE APP PROPERTIES:");
            //fecha o arquivo
            fos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}
